package com.outsource.framework.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.outsource.framework.domain.Tactivity;
import com.outsource.framework.domain.Thaoduan;
import com.outsource.framework.model.services.TactivityService;
import com.outsource.framework.model.services.ThaoduanService;
import com.outsource.framework.util.common.Response;

@Controller
public class ActivityContrller {

	@Autowired
	TactivityService service;
	
	@Autowired
	ThaoduanService tservice;
	
	@Autowired
	MessageSource messageSource;
	
	/**
	 * get jt activity 
	 * @param usermobile
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/WoWebActivityJTList",method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String, Object> WoWebActivityJTList(
			@RequestParam("usermobile")String usermobile,
			@RequestParam("type")String type,
			HttpServletRequest request){
		String status = "";
		String responseCode = getMessages("lable.code.success");
		String responseMessages = getMessages("lable.success");
		if(StringUtils.isNotBlank(type)&&type.equals("0"))
			status = "1";
		else
			status = "2";
		
		/* no pages
			List<Tactivity> responseEntry = service.getByJTAll(status);
		*/
		
		int page = ServletRequestUtils.getIntParameter(request, "page", 0);
		PageRequest pr = new PageRequest(page, 6, new Sort(Direction.DESC,"begintime"));
		Map<String, String> query = new HashMap<String, String>();
		query.put("level", "1");
		query.put("status", status);
		Page<Tactivity> pg = service.findQuery(query, pr);
		List<Tactivity> responseEntry = pg.getContent();
		Response response = new Response();
		Map<String, Object> responsemap = response.getResponse(responseCode, responseMessages, responseEntry);
		return responsemap;
	}
	
	/**
	 * get jt activity 
	 * @param usermobile
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/WoWebActivityPCList",method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String, Object> WoWebActivityPCList(
			@RequestParam("usermobile")String usermobile,
			@RequestParam("type")String type,
			HttpServletRequest request){
		String responseCode = getMessages("lable.code.success");
		String responseMessages = getMessages("lable.success");
		
		List<String> local = null;
		Thaoduan hd = tservice.findByAttribution(usermobile);
		/*if(hd!=null){
			local = new ArrayList<String>();
			local.add(hd.getProvince());
			local.add(hd.getCity());
		}else{
			local = new ArrayList<String>();
			local.add("0");
		}*/
		
		String status = "";
		if(StringUtils.isNotBlank(type)&&type.equals("0"))
			status = "1";
		else
			status = "2";
		
		List<String> level = new ArrayList<String>();
		level.add("2");
		level.add("3");
		
		int page = ServletRequestUtils.getIntParameter(request, "page", 0);
		PageRequest pr = new PageRequest(page, 10, new Sort(Direction.DESC,"begintime"));
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("level", level);
		query.put("status", status);
		if(hd!=null){
			query.put("province", hd.getProvince());
			query.put("city", hd.getCity());
		}
		
		Page<Tactivity> pg = service.findAllin(query, pr);
		
		/*List<Tactivity> responseEntry = service.getByPCAll(local,status);*/
		List<Tactivity> responseEntry = pg.getContent();
		Response response = new Response();
		Map<String, Object> responsemap = response.getResponse(responseCode, responseMessages, responseEntry);
		return responsemap;
	}
	
	/**
	 * get zh.properties content
	 * @param code
	 * @return
	 */
	public String getMessages(String code){
		return messageSource.getMessage(code, null,Locale.SIMPLIFIED_CHINESE);
	}
}
